package com.ruoyi.app.activity.mine

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import com.drake.net.Get
import com.drake.net.Put
import com.drake.net.utils.scopeNetLife
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.hjq.toast.ToastUtils
import com.ruoyi.app.api.ConfigApi
import com.ruoyi.app.api.OKHttpUtils
import com.ruoyi.app.databinding.ActivityEditInfoBinding
import com.ruoyi.app.model.entity.ResultEntity
import com.ruoyi.app.model.entity.UserInfoEntity
import com.ruoyi.app.model.entity.UserResult
import com.ruoyi.app.model.request.EditInfoRequest
import com.ruoyi.app.utils.FlowBus
import com.ruoyi.code.base.BaseBindingActivity
import com.ruoyi.code.witget.toast

class EditInfoActivity : BaseBindingActivity<ActivityEditInfoBinding>() {

    companion object {

        fun startActivity(context: Context) {
            val intent = Intent(context, EditInfoActivity::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }

    }

    override fun initView() {
        binding.titlebar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })
        binding.ivClearName.setOnClickListener {
            binding.etName.text.clear()
            it.visibility = View.INVISIBLE
        }
        binding.etName.addTextChangedListener {
            val txt = it.toString()
            if (!TextUtils.isEmpty(txt)) {
                binding.ivClearName.visibility = View.VISIBLE
            } else {
                binding.ivClearName.visibility = View.INVISIBLE
            }
        }
        binding.etMobile.addTextChangedListener {
            val txt = it.toString()
            if (!TextUtils.isEmpty(txt)) {
                binding.ivClearMobile.visibility = View.VISIBLE
            } else {
                binding.ivClearMobile.visibility = View.INVISIBLE
            }
        }
        binding.ivClearMobile.setOnClickListener {
            binding.etMobile.text.clear()
            it.visibility = View.INVISIBLE
        }

        binding.etEmail.addTextChangedListener {
            val txt = it.toString()
            if (!TextUtils.isEmpty(txt)) {
                binding.ivClearEmail.visibility = View.VISIBLE
            } else {
                binding.ivClearEmail.visibility = View.INVISIBLE
            }
        }
        binding.ivClearEmail.setOnClickListener {
            binding.etEmail.text.clear()
            it.visibility = View.INVISIBLE
        }

        binding.radioFemale.setOnClickListener {
            binding.radioMale.isChecked = false
            binding.radioFemale.isChecked = true
        }
        binding.radioMale.setOnClickListener {
            binding.radioMale.isChecked = true
            binding.radioFemale.isChecked = false
        }

        binding.btnSubmit.setOnClickListener {
            val name = binding.etName.text.toString().trim()
            if (TextUtils.isEmpty(name)) {
                binding.etName.requestFocus()
                ToastUtils.show("请输入昵称")
                return@setOnClickListener
            }
            val phone = binding.etMobile.text.toString().trim()
            if (TextUtils.isEmpty(phone)) {
                binding.etMobile.requestFocus()
                ToastUtils.show("请输入手机号")
                return@setOnClickListener
            }
            val email = binding.etEmail.text.toString().trim()
            if (TextUtils.isEmpty(email)) {
                binding.etEmail.requestFocus()
                ToastUtils.show("请输入邮箱")
                return@setOnClickListener
            }
            var sex = 0
            if (binding.radioMale.isChecked) {
                sex = 0
            }
            if (binding.radioFemale.isChecked) {
                sex = 1
            }
            val body = EditInfoRequest(name, email, phone, "" + sex)
            editProfile(body)
        }
    }

    override fun initData() {
        scopeNetLife {
            val body = Get<UserResult>(ConfigApi.getProfile).await()
            binding.etName.setText(body.data?.nickName)
            binding.etMobile.setText(body.data?.phonenumber)
            binding.etEmail.setText(body.data?.email)
            when (body.data?.sex) {
                "1" -> {
                    binding.radioMale.isChecked = false
                    binding.radioFemale.isChecked = true
                }

                "0" -> {
                    binding.radioMale.isChecked = true
                    binding.radioFemale.isChecked = false
                }
            }
        }
    }

    private fun editProfile(request: EditInfoRequest) {
        scopeNetLife {
            val body = Put<ResultEntity<String>>(ConfigApi.updateProfile) {
                body = OKHttpUtils.getRequestBody(request)
            }.await()
            if (body.code == ConfigApi.SUCESSS) {
                val userInfoEntity = UserInfoEntity()
                userInfoEntity.nickName = request.nickName
                FlowBus.withStick<UserInfoEntity>(FlowBus.update_user_info)
                    .post(lifecycleScope, userInfoEntity)
                finish()
            }
        }
    }

}
